package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.hotelManage.dao.HotelCommentDAO;
import com.wanmait.hotelManage.dao.PagerDAO;
import com.wanmait.hotelManage.util.DBHelper;
import com.wanmait.hotelManage.vo.HotelComment;
import com.wanmait.hotelManage.vo.Pager;

/**
 * Servlet implementation class HotelCommentServlet
 */
@WebServlet("/manage/HotelCommentServlet")
public class HotelCommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("deletehotelComment".equals(action)) {
			this.deletehotelComment(request, response);
		}else if("del".equals(action)) {
			this.del(request, response);
		}else if("update".equals(action)) {
			this.update(request, response);
		}else if("updates".equals(action)) {
			this.updates(request, response);
		}else if("initTwo".equals(action)) {
			this.initTwo(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("searchTwo".equals(action)) {
			this.searchTwo(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 搜索分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Integer pageNo=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(4);
		int rows=HotelCommentDAO.getHotelcommentdao().getrowss(text);
		pager.setPageCount(rows);
		List<HotelComment>HotelComments=HotelCommentDAO.getHotelcommentdao().findusername(text,pager);
		request.setAttribute("pager", pager);
		request.setAttribute("HotelComments", HotelComments);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/hotelComment/searchTwo.jsp").forward(request, response);
	}
	/**
	 * 搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		int rows=HotelCommentDAO.getHotelcommentdao().getrowss(text);
		pager.setPageCount(rows);
		List<HotelComment>HotelComments=HotelCommentDAO.getHotelcommentdao().findusername(text,pager);
		request.setAttribute("pager", pager);
		request.setAttribute("HotelComments", HotelComments);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/hotelComment/search.jsp").forward(request, response);
	}
	/**
	 * 分页界面跳转
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void initTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer no=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
		pager.setPageSize(4);
		pager.setPageNo(no);
		Integer rows=HotelCommentDAO.getHotelcommentdao().getrow();
		pager.setPageCount(rows);
		List<HotelComment>HotelComments=HotelCommentDAO.getHotelcommentdao().findall(pager);
		request.setAttribute("HotelComments", HotelComments);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/hotelComment/initTwo.jsp").forward(request, response);
	}
	/**
	 * 批量发布更新
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void updates(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ids=request.getParameter("ids");
		HotelCommentDAO.getHotelcommentdao().releases(ids);
	}
	/**
	 * 发布的更新
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		HotelCommentDAO.getHotelcommentdao().release(id);
	}
	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ids=request.getParameter("id");
		HotelCommentDAO.getHotelcommentdao().del(ids);
	}
	/**
	 * 普通删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void deletehotelComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		HotelCommentDAO.getHotelcommentdao().update(id);
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		Integer rows=HotelCommentDAO.getHotelcommentdao().getrow();
		pager.setPageCount(rows);
		List<HotelComment>HotelComments=HotelCommentDAO.getHotelcommentdao().findall(pager);
		request.setAttribute("HotelComments", HotelComments);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/hotelComment/list.jsp").forward(request, response);
	}
}
